import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/model/GetUserPublicInfoByCodeResultData.dart';
import 'package:easy_chat_app/commons/utils/HttpUtils.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/widget/ConfirmDialog.dart';
import 'package:easy_chat_app/commons/widget/UserHeadPortrait.dart';
import 'package:easy_chat_app/mobile/constants/MobileAppGlobalConstants.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MobileUserPublicInfoPageParams {
  GetUserPublicInfoByCodeResultData userPublicInfo;

  MobileUserPublicInfoPageParams.create({required this.userPublicInfo});
}

class MobileUserPublicInfoPageChangeNotifier extends ChangeNotifier {}

class MobileUserPublicInfoPage extends StatelessWidget {
  MobileUserPublicInfoPage({super.key});

  final Logger logger = Logger();

  @override
  Widget build(BuildContext context) {
    MobileUserPublicInfoPageParams pageParams =
        RouteUtils.currentRouteSettings().arguments
            as MobileUserPublicInfoPageParams;
    GetUserPublicInfoByCodeResultData userPublicInfo =
        pageParams.userPublicInfo;
    return Consumer<AppUserDetailChangeNotifier>(
        builder: (context, appUserDetailChangeNotifier, child) {
      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) {
            return MobileUserPublicInfoPageChangeNotifier();
          })
        ],
        builder: (context, child) {
          return Scaffold(
            appBar: AppBar(
              surfaceTintColor: MobileAppGlobalConstants.appBarSurfaceTintColor,
              backgroundColor: MobileAppGlobalConstants.appBarBackgroundColor,
            ),
            body: Container(
              color: const Color.fromRGBO(237, 237, 237, 1),
              child: Column(
                children: [
                  Container(
                    color: Colors.white,
                    height: 35,
                  ),
                  Container(
                    color: Colors.white,
                    child: Row(
                      children: [
                        const SizedBox(
                          width: 25,
                        ),
                        UserHeadPortrait.network(
                            size: 65,
                            userCode: userPublicInfo.code!,
                            userHeadPortrait: userPublicInfo.headPortrait!,
                            userHeadPortraitStoreType:
                                userPublicInfo.headPortraitStoreType!),
                        const SizedBox(
                          width: 20,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Text(
                              userPublicInfo.nickname ?? "",
                              style: const TextStyle(
                                  fontSize: 20, color: Colors.black),
                            ),
                            Text(
                              "EC号: ${userPublicInfo.code ?? ""}",
                              style: const TextStyle(
                                  fontSize: 14,
                                  color: Color.fromRGBO(115, 115, 115, 1)),
                            )
                          ],
                        )
                      ],
                    ),
                  ),
                  Container(
                    color: Colors.white,
                    height: 30,
                  ),
                  const SizedBox(
                    height: 8,
                  ),
                  GestureDetector(
                    child: Container(
                      width: double.infinity,
                      height: 55,
                      alignment: Alignment.center,
                      color: Colors.white,
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.chat_bubble_outline,
                            color: Color.fromRGBO(87, 107, 149, 1),
                            size: 18,
                          ),
                          SizedBox(
                            width: 5,
                          ),
                          Text(
                            "添加到通讯录",
                            style: TextStyle(
                                fontSize: 16,
                                color: Color.fromRGBO(87, 107, 149, 1)),
                          )
                        ],
                      ),
                    ),
                    onTap: () async {
                      await _sendFriendRequest(context, userPublicInfo);
                    },
                  )
                ],
              ),
            ),
          );
        },
      );
    });
  }

  Future<void> _sendFriendRequest(BuildContext context,
      GetUserPublicInfoByCodeResultData userPublicInfo) async {
    bool result = await ConfirmDialog.show(context,
        content: Text("确定把 ${userPublicInfo.nickname} 添加到通讯录么?"));
    if (result) {
      var result = await HttpUtils.createFriendRequest(userPublicInfo.id!);
      logger.i("result:$result");
    }
  }
}
